//
//  FPProtocolModel.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/13.
//

#import "FPProtocolModel.h"

@implementation FPProtocolModel

+ (JSONKeyMapper *)keyMapper {
    
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"ID" : @"id"}];
}

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
    
    return YES;
}



#pragma mark - 获取协议
+ (void)getProtocolWithType:(FPProtocolType)type success:(void (^)(NSInteger, NSString * _Nonnull, NSArray *))success failure:(void (^)(NSError * _Nonnull))failure {
    
    
    NSMutableArray *params = NSMutableArray.array;
    
    switch (type) {
        /*
         Authentication 电子认证      位于实名认证    协议列表
         BindingCard   贷款合同  位于确认借款借款详细页面    合同列表
         Cancellation  注销协议   位于注销页面
         CreditInvestigation  个人征信及用户信息授权书   位于贷款申请页面
         FaceVerification人 脸验证授权协议    位于实名认证页面
         LoanContract 绑卡及资金划扣服务协议   位于添加银行卡页面
         PersonalInformation 用户个人信息授权书（疑似重复，可与CreditInvestigation共用）  位于智能匹配、资料填写、实名认证页面
         RegistrationAgreement 众鑫花注册协议    位于注册页面
         StudentIdentity 非学生身份承诺函   位于贷款申请页面
         PrivacyPolicy 隐私政策
         */
            
        case FPProtocolTypeListController: // 我的-> 查看协议列表
            
            // 空
            break;
            
        case FPProtocolTypeFirstEntry:
            
            [params addObject:@"PrivacyPolicy"];
            [params addObject:@"RegistrationAgreement"];
            
            break;
        
        case FPProtocolTypeLogin: // 登录
            
            [params addObject:@"PrivacyPolicy"];
            [params addObject:@"RegistrationAgreement"];
            
            break;
            
        case FPProtocolTypeBankCard: // 银行卡
            
            [params addObject:@"BindingCard"];
            break;
            
        case FPProtocolTypeLoan: // 借款
            
            [params addObject:@"CreditInvestigation"];
            [params addObject:@"PersonInfo"];
            break;
            
        case FPProtocolTypeAuthentic: // 实名认证
            
            [params addObject:@"Authentication"];
            [params addObject:@"FaceVerification"];
            [params addObject:@"PersonalInformation"];
            [params addObject:@"StudentIdentity"];
            break;
            
        case FPProtocolTypeConfirmContract: // 确认借款页面
            
            [params addObject:@"LoanContract"];
            break;
            
        case FPProtocolTypeCleanAccount: // 注销
            
            [params addObject:@"Cancellation"];
            break;
            
        case FPProtocolTypeUserProfile2: // 留资2
            
            [params addObject:@"Sensitive"];
            break;
            
        default:
            break;
    }    
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&parseError];
    
    NSString *jsonstr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSData *objectData = [jsonstr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&parseError];
    
//    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    // 渠道号
//    [param setObject:@"" forKey:@"channelNumber"];
    // 1: iOS
//    [param setObject:@"1" forKey:@"deviceType"];
//    NSString *typeStr = [NSString stringWithFormat:@"%zd",type];
//    [param setObject:typeStr forKey:@"protocolModuleLocation"];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:PROTOCOL_INFO_URL] parameters:jsonDic type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSArray *list = [FPProtocolModel arrayOfModelsFromDictionaries:[object objectForKey:@"data"] error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message , list);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

#pragma mark - 签署协议
+ (void)protocolSignWithParams:(NSArray *)params success:(void (^)(NSInteger, NSString * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {

    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&parseError];
    
    NSString *jsonstr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSData *objectData = [jsonstr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&parseError];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:PROTOCOL_SIGN_URL] parameters:jsonDic type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}


#pragma mark - 查看已签署协议的信息
+ (void)getProtocolSignInfoWithID:(NSString *)ID success:(void (^)(NSInteger, NSString * _Nonnull, NSArray * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    
    [param setObject:ID forKey:@"agreementId"];
    
    NSString *urlStr = [APIClient getQueryURLStringWithParam:param URLString:GET_PROTOCOL_SIGN_URL];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:urlStr] parameters:@{} type:HttpRequestTypeGet success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSArray *list = [FPProtocolModel arrayOfModelsFromDictionaries:[object objectForKey:@"data"] error:&error];
        
        for (FPProtocolModel *model in list) {
            
            model.userPhone = [FPUserModel AES256Decrypt:model.userPhone];
            model.idNumber = [FPUserModel AES256Decrypt:model.idNumber];
        }
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message , list);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}


@end
